/* eslint-disable @typescript-eslint/ban-types */
/**
 * date:2023.05.22
 * add by Eric
 * 通用确认取消弹窗。
 * 支持0,1,2个按钮。
 * 描述文本支持滑动。
 * 用法：
 * CommonTipWnd.Show(
 * {
 * btnActiveNum:2,
 * titleStr:"充值结果",
 * infoStr:"充值成功，订单号" + payId,
 * clickMaskClose:true
 * })
 * 此界面不处理多语言，按钮问本默认英文confirm，canel。在外部处理好多语言再传进来
 */

import { CommonUtils } from "./CommonUtils";
import { EventManager } from "./EventManager";
import { EventType } from "./EventType";

const { ccclass, property, menu } = cc._decorator;
const btnDefaultX = 170;
// 描述文本对齐模式
export enum HAlignType {
    LEFT = 0,
    Center,
    Right,
}
export interface TipData {
    btnActiveNum?: number;
    closeActive?: boolean;

    // 当前有两套背景，传值。默认使用第0套
    // useBgIndex?: number;

    // 按钮和图片纹理，只可配置一种，都配置只认sprite. 都不配置显示sprite
    // 如果后续需要按钮文本自定义颜色，可以再加，暂时不需要
    btnLTxt?: string;
    btnRTxt?: string;
    btnLSpr?: cc.SpriteFrame;
    btnRSpr?: cc.SpriteFrame;
    // 两个按钮距离中点左右偏移量，两个按钮都显示时生效
    btnSpaceX?: number;

    btnLCB?: Function;
    btnRCB?: Function;
    // 界面关闭回调
    closeCB?: Function;

    // 文本标题，在外部转好传进
    titleStr?: string;
    // 图片标题，在外部多语言转好传sprite进来。
    titleFrame?: cc.SpriteFrame;
    // 描述文本，放在scrollview中
    infoStr?: string;
    infoFontSize?: number;
    infoFontHorizontalAlign?: HAlignType;

    clickMaskClose?: boolean;
}

@ccclass
export class CommonTipWnd extends cc.Component {
    @property(cc.Sprite)
    spriteTitle: cc.Sprite = null;

    @property(cc.Label)
    labelTitle: cc.Label = null;

    @property(cc.RichText)
    infoLabel: cc.RichText = null;

    @property(cc.Node)
    btnL: cc.Node = null;

    @property(cc.Node)
    btnR: cc.Node = null;

    @property(cc.Node)
    btnClose: cc.Node = null;

    @property(cc.Node)
    bg: cc.Node = null;

    @property([cc.SpriteFrame])
    bgSpriteList: Array<cc.SpriteFrame> = [];

    @property([cc.Vec2])
    closePosList: Array<cc.Vec2> = [];

    private _btnLCallback: Function = null;
    private _btnRCallback: Function = null;
    private _closeCallback: Function = null;
    /** 点击周边区域关闭，默认为false */
    private _clickMaskClose = false;

    public static async Show(data: TipData) {
        const wnd = (await CommonUtils.GetInstance().openPrefab('prefab/CommonTipWnd')) as cc.Node;
        if (!wnd) return;
        wnd.parent = cc.find('Canvas');
        wnd.getComponent('CommonTipWnd').UpdateWnd(data);
    }

    protected onLoad(): void {
        // if(this.spriteTitle.active)
        CommonUtils.GetInstance().BtnChildAddClickEvent('content/btnClose', this.node, this.closeLayer, this);
        CommonUtils.GetInstance().BtnChildAddClickEvent('content/btnR', this.node, this.onClickBottomBtn, this);
        CommonUtils.GetInstance().BtnChildAddClickEvent('content/btnL', this.node, this.onClickBottomBtn, this);
    }

    public onEnable(): void {
        console.log(`CommonTipWnd OnOpen`);
        EventManager.Instance.on(EventType.CLOSE_COMMON_TIP_WND, this.closeLayer, this);
    }

    public onDisable(): void {
        EventManager.Instance.off(EventType.CLOSE_COMMON_TIP_WND, this.closeLayer, this);
    }

    public UpdateWnd(data: TipData) {
        this.infoLabel.horizontalAlign = (data.infoFontHorizontalAlign as number) || 0;
        this.infoLabel.fontSize = data.infoFontSize || 30;

        this.spriteTitle.spriteFrame = data.titleFrame || null;
        this.labelTitle.string = data.titleStr || '';
        // 默认英文，多语言请从外部传入。
        const enSureStr = 'confirm';
        const canelStr = 'canel';

        const labelL = this.btnL.getChildByName('Label');
        const sprL = this.btnL.getChildByName('spr');
        const labelR = this.btnR.getChildByName('Label');
        const sprR = this.btnR.getChildByName('spr');
        switch (data.btnActiveNum || 0) {
            case 0:
                this.btnL.active = false;
                this.btnR.active = false;
                break;
            case 1:
                this.btnL.active = false;
                this.btnR.active = true;
                this.btnR.x = 0;
                break;
            case 2:
                this.btnL.active = true;
                this.btnR.active = true;
                this.btnL.x = -data.btnSpaceX || -btnDefaultX;
                this.btnR.x = data.btnSpaceX || btnDefaultX;
                break;
            default:
                break;
        }

        if (data.btnLSpr) {
            sprL.getComponent(cc.Sprite).spriteFrame = data.btnLSpr;
        } else {
            labelL.getComponent(cc.Label).string = data.btnLTxt || canelStr;
        }

        labelL.active = !!data.btnLSpr;
        sprL.active = !labelL.active;

        if (data.btnRSpr) {
            sprR.getComponent(cc.Sprite).spriteFrame = data.btnLSpr;
        } else {
            labelR.getComponent(cc.Label).string = data.btnRTxt || enSureStr;
        }
        labelR.active = !!data.btnRSpr;
        sprR.active = !labelL.active;

        this.btnClose.active = data.closeActive;

        // 背景图
        // if (!data.useBgIndex) data.useBgIndex = 0;
        // this.bg.getComponent(cc.Sprite).spriteFrame = this.bgSpriteList[data.useBgIndex];
        // this.btnClose.setPosition(this.closePosList[data.useBgIndex]);

        this.infoLabel.string = data.infoStr || '';

        this._btnLCallback = data.btnLCB;
        this._btnRCallback = data.btnRCB;
        this._closeCallback = data.closeCB;

        this._clickMaskClose = data.clickMaskClose;
    }

    public OnCloseBtnClick(event: cc.Event) {
        if (event.target.name == 'Mask' && !this._clickMaskClose) {
            return;
        }
        this.closeLayer();
    }

    public closeLayer() {
        if (this._closeCallback) {
            this._closeCallback();
        }
        this.node.removeFromParent();
    }

    public onClickBottomBtn(event: cc.Event) {
        switch (event.target.name) {
            case 'btnR':
                // eslint-disable-next-line no-unused-expressions
                this._btnRCallback ? this._btnRCallback() : this.closeLayer();
                break;
            case 'btnL':
                // eslint-disable-next-line no-unused-expressions
                this._btnLCallback ? this._btnLCallback() : this.closeLayer();
                break;
            default:
                break;
        }
    }
}
